import os
import configparser

CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'config.ini')

def get_config():
    """读取配置文件"""
    config = configparser.ConfigParser()
    config.read(CONFIG_FILE, encoding='utf-8')
    return config

def save_comfyui_path(new_path: str):
    """保存ComfyUI路径到配置文件"""
    config = get_config()
    if 'Paths' not in config:
        config['Paths'] = {}
    
    # 转换为相对路径
    # rel_path = os.path.relpath(new_path, os.path.dirname(__file__))
    # config['Paths']['comfyui_folder'] = rel_path
    config['Paths']['comfyui_folder'] = new_path
    
    with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
        config.write(f)

def get_config_comfyui_path() -> str:
    """获取config.ini中[Paths]下Comfyui_folder的路径"""
    config = get_config()
    return config['Paths']['comfyui_folder']

def get_nvidia_gpu_script_path() -> str:
    """获取run_nvidia_gpu.bat启动脚本的路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "run_nvidia_gpu.bat")

def get_checkpoint_dir_path() -> str:
    """获取checkpoints文件夹路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "ComfyUI", "models", "checkpoints")

def get_unet_dir_path() -> str:
    """获取unet文件夹路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "ComfyUI", "models", "unet")

def get_lora_dir_path() -> str:
    """获取loras文件夹路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "ComfyUI", "models", "loras")

def get_clip_dir_path() -> str:
    """获取clip文件夹路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "ComfyUI", "models", "clip")

def get_input_path() -> str:
    """获取input文件夹路径"""
    base_path = get_config_comfyui_path()
    return os.path.join(base_path, "ComfyUI", "input")

def get_output_path() -> str:
    """获取output文件夹路径"""
    # 改为项目内统一的输出目录
    return get_local_output_images_path()

def get_local_output_images_relative_path() -> str:
    """返回项目内输出图片文件夹的相对路径（相对项目根目录）"""
    return "output_images"

def get_local_output_images_path() -> str:
    """获取项目内 output_images 文件夹的绝对路径"""
    # 项目根目录：config.py 的上级目录
    project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    return os.path.join(project_root, get_local_output_images_relative_path())

def get_config_baidu_fanyi_app_id() -> str:
    """获取config.ini中[baidu_fanyi]下app_id"""
    config = get_config()
    return config['baidu_fanyi']['app_id']

def get_config_baidu_fanyi_app_key() -> str:
    """获取config.ini中[baidu_fanyi]下app_key"""
    config = get_config()
    return config['baidu_fanyi']['app_key']




